import token from 'jsonwebtoken';
import tokenConfig from '../config/token';
import type SysUser from '../../typings/sysUser';

export const create = function (data: any) {
    return token.sign(data, tokenConfig.key, { expiresIn: tokenConfig.expire });
};

const parse = function (tokenString: string): null | SysUser {
    return token.decode(tokenString) as SysUser;
};

export const verify = function (tokenString: string) {
    try {
        token.verify(tokenString, tokenConfig.key);
        return parse(tokenString);
    } catch (error) {
        return false;
    }
};
